package org.footballmanager.pandafootball.system.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.footballmanager.pandafootball.common.util.SpiderUtil;
import org.footballmanager.pandafootball.system.dao.ImageDao;
import org.footballmanager.pandafootball.system.domain.Image;
import org.footballmanager.pandafootball.system.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Slf4j
@Service
public class ImageServiceImpl extends ServiceImpl<Image> implements ImageService {
    @Autowired
    private ImageDao imageDao;

    /**
     * 根据状态和条目数获取数据
     *
     * @param status 状态
     * @param limit  条目数
     * @return
     */
    @Override
    public List<Image> getStatusAndNumber(int status, int limit) {
        Example example = new Example(Image.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", status);
        return imageDao.selectByExampleAndRowBounds(example, new RowBounds(0, limit));
    }

    //@Scheduled(cron = "* */1 * * * ?")
    @Override
    public void getImage(){
        log.info("process qy");
        List<Image> imageList = this.getStatusAndNumber(0, 23);
        if(imageList != null && !imageList.isEmpty()){
            for(Image image: imageList){
                boolean ok = SpiderUtil.processImage(image.getUrl(), image.getName());
                image.setProcess(ok);
                image.setStatus(1);
                this.updateNotNull(image, null);
            }
        }
    }
}
